# coding=UTF-8
import os
import sys
import time
import json
from common.entitys.caiptal import TransCode1

father_path = os.path.abspath(os.path.dirname(os.getcwd()) + os.path.sep + ".")
sys.path.append(father_path)
from service import data_service
from utils import http_util


# 实时信息
def rt_info(code) -> dict:
    # 原地址 url = 'http://push2.eastmoney.com/api/qt/stock/get?ut=fa5fd1943c7b386f172d6893dbfba10b&invt=2&fltt=2&fields=f43,f57,f58,f169,f170,f46,f44,f51,f168,f47,f164,f163,f116,f60,f45,f52,f50,f48,f167,f117,f71,f161,f49,f530,f135,f136,f137,f138,f139,f141,f142,f144,f145,f147,f148,f140,f143,f146,f149,f55,f62,f162,f92,f173,f104,f105,f84,f85,f183,f184,f185,f186,f187,f188,f189,f190,f191,f192,f107,f111,f86,f177,f78,f110,f262,f263,f264,f267,f268,f250,f251,f252,f253,f254,f255,f256,f257,f258,f266,f269,f270,f271,f273,f274,f275,f127,f199,f128,f193,f196,f194,f195,f197,f80,f280,f281,f282,f284,f285,f286,f287,f292&secid=1.600031&cb=jQuery1124011433386506447552_1630466933603&_=1630466933612'
    time_stamp = int(round(time.time() * 1000))
    url_rt = 'http://push2.eastmoney.com/api/qt/stock/get?ut=fa5fd1943c7b386f172d6893dbfba10b' \
             '&invt=2&fltt=2&fields=f43,f57,f58,f169,f170,f46,f44,f51,f168,f47,f164,f163,f116,f60,' \
             'f45,f52,f50,f48,f167,f117,f71,f161,f49,f530,f135,f136,f137,f138,f139,f141,f142,f144,f145,' \
             'f147,f148,f140,f143,f146,f149,f55,f62,f162,f92,f173,f104,f105,f84,f85,f183,f184,f185,f186,f187,' \
             'f188,f189,f190,f191,f192,f107,f111,f86,f177,f78,f110,f262,f263,f264,f267,f268,f250,f251,f252,f253,' \
             'f254,f255,f256,f257,f258,f266,f269,f270,f271,f273,f274,f275,f127,f199,f128,f193,f196,f194,f195,f197,f80,' \
             'f280,f281,f282,f284,f285,f286,f287,f292&secid=!!!!!.@@@@@&cb=jQuery1124011433386506447552_#####&_=$$$$$'
    url_rt = url_rt.replace('!!!!!', data_service.stock(code).get('exchange')) \
        .replace('@@@@@', code).replace('#####', str(time_stamp)).replace('$$$$$', str(time_stamp + 9))
    # resp_json = json.loads(str(requests.get(url_rt).content.decode('utf-8')).split('(')[1].split(')')[0]).get('data')
    resp_content = str(http_util.request_get(url_rt).content.decode('utf-8'))
    return json.loads(resp_content[resp_content.find('(') + 1:resp_content.rfind(')')]).get('data')


# 批量实时信息
def rt_info_mul():
    # url_mul = '	http://92.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112406935912479304673_1630489335173&pn=1&pz=20&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:0 t:6,m:0 t:80,m:1 t:2,m:1 t:23&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_=1630489335174'
    stock_dict = {}
    for i in range(100):
        url_mul = '	http://92.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112406935912479304673_!!!!!' \
                  '&pn=@@@@@&pz=10000&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:0 t:6,m:0 t:80,m:1 t:2,' \
                  'm:1 t:23&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,' \
                  'f25,f22,f11,f62,f128,f136,f115,f152&_=#####'
        time_stamp = int(round(time.time() * 1000))
        url_mul = url_mul.replace('!!!!!', str(time_stamp)).replace('@@@@@', str(i + 1)) \
            .replace('#####', str(time_stamp + 9))
        resp_str = str(http_util.request_get(url_mul).content.decode('utf-8'))
        resp_json = json.loads(resp_str[resp_str.find('(') + 1:resp_str.rfind(')')]).get('data')
        if resp_json is None:
            break
        for info in resp_json.get('diff'):
            code = info.get(TransCode1.code)
            if code[0] == '00' or code[0] == '60' or code[0] == '30' or code[0] == '68':
                data_service.stock(code)
                stock_dict[code] = info
    return stock_dict


def real_time_price(stock_code, stock_exchange, size):
    time_stamp = int(round(time.time() * 1000))
    path_url = 'http://push2.eastmoney.com/api/qt/stock/details/get?' \
               'ut=fa5fd1943c7b386f172d6893dbfba10b&fields1=f1,f2,f3,f4&' \
               'fields2=f51,f52,f53,f54,f55&' \
               'pos=-' + str(size) + \
               '&secid=' + str(stock_exchange) + '.' + str(stock_code) + \
               '&cb=jQuery1124011692851191143305_' + str(time_stamp) + '&_=' + str(time_stamp + 103)
    # print(path_url)
    resp = http_util.request_get(path_url)
    resp_content = str(resp.content.decode('utf-8'))
    days_info_str = resp_content.split('(')[1].split(')')[0]
    resp_json = json.loads(days_info_str)
    return resp_json['data']['details']


# 分_k 价格
def minutes_k(stock_code, stock_exchange, days):
    time_stamp = int(round(time.time() * 1000))
    # 实时价格url
    path_url = 'http://push2.eastmoney.com/api/qt/stock/trends2/get?' + \
               'fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13' + \
               '&fields2=f51,f52,f53,f54,f55,f56,f57,f58' + \
               '&ut=fa5fd1943c7b386f172d6893dbfba10b&ndays=' + str(days) + \
               '&iscr=0&' \
               'secid=' + str(stock_exchange) + '.' + str(stock_code) + \
               '&cb=jQuery1124030062123329295565_' + \
               str(time_stamp) + '&_=' + str(time_stamp + 41)
    resp = http_util.request_get(path_url)
    resp_content = str(resp.content.decode('utf-8'))
    days_info_str = resp_content.split('(')[1].split(')')[0]
    resp_json = json.loads(days_info_str)
    return resp_json['data']['trends']


#  获取实时价格
def get_rt_p(stock_code: str) -> float:
    dict = data_service.stock(stock_code)
    details = real_time_price(stock_code, dict['exchange'], 1)
    if len(details) == 0:
        return 0
    return float(details[0].split(',')[1])


# 获取含有长度的价格
def get_rt_ps(stock_code, size):
    dict = data_service.stock(stock_code)
    return real_time_price(stock_code, dict['exchange'], size)


if __name__ == '__main__':
    # print(get_rt_p('002594'))
    # print(real_time_price('002594', 0, 2))
    print(rt_info('600031'))
    print(rt_info_mul())
